
package grails.coherence.support
import groovy.util.Expando;

import grails.test.*
import groovy.mock.interceptor.*;
import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;
import com.tangosol.util.Filter;
import com.tangosol.util.SafeHashMap;
import com.tangosol.util.InvocableMap.EntryAggregator;
import com.tangosol.util.InvocableMap.EntryProcessor;
import com.tangosol.util.filter.AlwaysFilter;

class CoherenceSupportTests extends GrailsUnitTestCase {
	
	def testClosure = { all() }
	
	protected void setUp() {
		super.setUp()
		CoherenceSupport.registerMethods.call()
	}
	
	protected void tearDown() {
		super.tearDown()
	}
	
	void testEntrySet() {
		
		def namedCache = [ 'entrySet' : { Filter filter -> 
			assert filter == AlwaysFilter.INSTANCE
		}] as NamedCache
		
		namedCache.entrySet(testClosure)
	}
	
	void testEntrySetWithComparator() {
		
		def comparator = {} as Comparator
		
		def namedCache = [ 'entrySet' : { Filter filter, Comparator c -> 
			assert filter == AlwaysFilter.INSTANCE
			assert c == comparator
		}] as NamedCache
		
		namedCache.entrySet(comparator, testClosure)
	}
	
	void testKeySet() {
		
		def namedCache = [ 'keySet' : { Filter filter -> 
			assert filter == AlwaysFilter.INSTANCE
		}] as NamedCache
		
		namedCache.keySet(testClosure)
	}
	
	void testValues() {
		
		def entries = [
		    [key:'k1', value:'v1'],
		    [key:'k2', value:'v2'],
		] 
		
		def namedCache = [ 'entrySet' : { Filter filter -> 
			assert filter == AlwaysFilter.INSTANCE
			return new LinkedHashSet(entries)
		}] as NamedCache
		
		assert entries*.value == namedCache.values(testClosure)
	}
	
	void testValuesWithComparator() {
		
		def comparator = {} as Comparator
		
		def entries = [
		[key:'k1', value:'v1'],
		[key:'k2', value:'v2'],
		] 
		
		def namedCache = [ 'entrySet' : { Filter filter, Comparator c -> 
			assert filter == AlwaysFilter.INSTANCE
			assert c == comparator
			return new LinkedHashSet(entries)
		}] as NamedCache
		
		assert entries*.value == namedCache.values(comparator, testClosure)
	}
	
	void testInvokeAll() {
		
		def p = {} as EntryProcessor
		
		def namedCache = [ 'invokeAll' : { Filter filter, EntryProcessor processor -> 
			assert filter == AlwaysFilter.INSTANCE
			assert processor == p
		}] as NamedCache
		
		namedCache.invokeAll(p, testClosure)
	}
	
	void testAggregate() {
		
		def a = {} as EntryAggregator
		
		def namedCache = [ 'aggregate' : { Filter filter, EntryAggregator aggregator -> 
			assert filter == AlwaysFilter.INSTANCE
			assert aggregator == a
		}] as NamedCache
		
		namedCache.aggregate(a, testClosure)
	}
			
}